package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.*;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderInfo;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.vo.OrderSubmitParam;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约控制器
 */
@RestController
@RequestMapping("/mobile/order")
@Slf4j
@Api(tags = "手机端订单接口")
@Transactional
public class MobileOrderController {
    @Autowired
    private OrderService orderService;
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("找通过id",id);
        OrderInfo order = orderService.findId(id);
        return new Result(true,"查询成功",order);
    }

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MemberService memberService;


    @Autowired
    private OrderSettingService orderSettingService;

    @Transactional
    @PostMapping("/submit")
    public Result submit(@RequestBody OrderSubmitParam orderSubmitParam) {

        // 接收前端传递过来的验证码作比较
        String validateCode = orderSubmitParam.getValidateCode();
        // 获取手机号
        String telephone = orderSubmitParam.getTelephone();
        // 获取生成的验证码
        String code = null;

        // 又没生成验证码直接提交的情况
        try {
            code = redisTemplate.opsForValue().get(telephone).toString();
        } catch (NullPointerException e) {
            log.info("程序空指针异常,用户没获取验证码直接登录");
            return new Result(false, "请先获取验证码");
        }

        if (code == null || !(code.equals(validateCode))) {

            // 输入的验证码错误
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }
        // redis中删除验证码
        redisTemplate.delete(telephone);

        // 判断是否输入预约日期
        if (orderSubmitParam.getOrderDate() == null) {

            // 提交的表单没有输入预约日期,返回去
            return new Result(false, MessageConst.SELECTED_DATE_CANNOT_ORDER);
        }

        // 登录成功,去数据库查询是否是新用户
        memberService.submit(orderSubmitParam);

        // 查询用户id
        int userId = memberService.selectByPhone(orderSubmitParam.getTelephone());

        log.info("用户id:" + userId);

        // 查询提交预约日期是否重复
        Order flag = orderService.selectOrdeDate(userId, orderSubmitParam.getOrderDate(), orderSubmitParam.getSetMealId());
        if (flag != null) {

            // 查询到的日期和预约的日期相同,返回false
            return new Result(false, MessageConst.HAS_ORDERED);
        }

        // 判断是否约满
        int number = orderSettingService.getNumber(orderSubmitParam.getOrderDate());
        if (number == 0) {

            // 已经约满,返回false
            return new Result(false, MessageConst.ORDER_FULL);
        }

        // 保存订单到数据库
        int id = orderService.add(orderSubmitParam,userId);
        log.info("返回id" + id);
        Order order = orderService.seleceByIdupdata(id);
        log.info("order------------------------------------"+order);

        // 更新预约人数
        orderSettingService.updaraNumber(order.getOrderDate());

        // 完成业务,返回
        return new Result(true, MessageConst.ORDER_SUCCESS, order);
    }

}
